package com.yswee.framework.widget.adapterview;

import java.util.ArrayList;

import com.yswee.framework.common.cache.image.ImageFetcher;
import com.yswee.framework.data.adapter.ILayoutViewAdapter;
import com.yswee.framework.data.adapter.LayoutViewAdapter;
import com.yswee.framework.widget.layoutview.ILayoutView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Interpolator;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;

public abstract class MHorizontalListView<T> extends com.yswee.framework.widget.base.MHorizontalListView {
	
	public static final int LISTVIEW_TYPE_VIEW = 0;
	
	protected ILayoutViewAdapter<T> mAdapter;
	protected OnScrollListener mScrollListener;
	
	protected OnScrollListener mScrollListenerProxy = new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                ImageFetcher.get().setPauseWork(true);
            } else {
            	ImageFetcher.get().setPauseWork(false);
            }
            if(mScrollListener != null) {
            	mScrollListener.onScrollStateChanged(view, scrollState);
            }
        }
        
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, 
        		int visibleItemCount, int totalItemCount) {
            if(mScrollListener != null) {
            	mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        }
    };
	
	public MHorizontalListView(Context context) {
		this(context, null);
	}
	
	public MHorizontalListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeListView();
		initializeAdapter();
	}
	
	public MHorizontalListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initializeListView();
		initializeAdapter();
	}

	protected void initializeListView() {
		setHorizontalScrollBarEnabled(true);
		setVerticalScrollBarEnabled(false);
		hideFadingEdge();
//		super.setOnScrollListener(mScrollListenerProxy);
		if(getListViewType() == LISTVIEW_TYPE_VIEW) {
			mAdapter = new LayoutViewAdapter<T>(mContext) {
				@Override
				public int getViewTypeCount() {
					return MHorizontalListView.this.getViewTypeCount();
				}
	
				@Override
				protected int getLayoutResId(int position, T t, int itemViewType) {
					return MHorizontalListView.this.getLayoutResId(position, t, itemViewType);
				}

				@Override
				protected ILayoutView<T> getLayoutView(int position, T t,
						int itemViewType) {
					return MHorizontalListView.this.getLayoutView(position, t, itemViewType);
				}

				@Override
				protected boolean enabledSwipeMenu() {
					return false;
				}

				@Override
				protected boolean enabledSwipeMenu(int position) {
					return false;
				}

				@Override
				protected int getMenuResId(int position, T t, int itemViewType) {
					return 0;
				}

				@Override
				protected ILayoutView<T> getLayoutMenu(int position, T t, int itemViewType) {
					return null;
				}

				@Override
				protected Interpolator getOpenInterpolator() {
					return null;
				}

				@Override
				protected Interpolator getCloseInterpolator() {
					return null;
				}
			};
		} else {
			onInvalidParam("getListViewType() return invalid value");
		}
	}
	
	/**
	 * 初始化adapter
	 */
	protected void initializeAdapter() {
		setAdapter(mAdapter);
	}

	/*=============== protected method ====================*/
	/**
	 * 参数错误
	 * @param msg
	 */
	protected void onInvalidParam(String msg) {
		throw new RuntimeException(msg);
	}
	
	/**
	 * ListView的模板数
	 * @return
	 */
	protected int getViewTypeCount() {
		return 1;
	}
    
	/**
	 * ListView的加载方式
	 * LISTVIEW_TYPE_VIEW  (必须override createLayoutView 方法）
	 * LISTVIEW_TYPE_DATABINDING （必须override getLayoutResId/getLayoutView 方法）
	 * @return
	 */
	protected abstract int getListViewType();
    
	/*=============== public method ====================*/
	/**
	 * 设置数据源
	 * @param data
	 */
	public void setDataSource(ArrayList<T> data) {
		mAdapter.setDataSource(data);
		mAdapter.notifyDataSetChanged();
	}

	/**
	 * 取得数据源
	 * @return
	 */
	public ArrayList<T> getDataSource() {
		if(mAdapter != null)
			return mAdapter.getDataSource();
		else
			return null;
	}

	/**
	 * 通知adapter刷新UI
	 */
	public void notifyDataSetChanged() {
		if(mAdapter != null)
			mAdapter.notifyDataSetChanged();
	}
	
	/**
	 * 获取数据源的条数
	 */
	public int getAdapterCount() {
		ListAdapter adapter = getAdapter();
		if(adapter != null) {
			if(adapter instanceof HeaderViewListAdapter) {
				adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter(); 
			}
			return adapter.getCount();
		}
		return 0;
	}
	
	/**
	 * 本地数据源增加数据
	 * @param t
	 * @return
	 */
	public boolean addData(T t) {
		ArrayList<T> datas = getDataSource();
		if(datas != null) {
			datas.add(t);
			setDataSource(datas);
			return true;
		}
		return false;
	}
	
	/**
	 * 本地数据源增加数据
	 * @param index
	 * @param t
	 * @return
	 */
	public boolean addData(int index, T t) {
		ArrayList<T> datas = getDataSource();
		if(datas != null) {
			datas.add(index, t);
			setDataSource(datas);
			return true;
		}
		return false;
	}
	
	/**
	 * 本地数据源移除数据
	 * @param t
	 * @return
	 */
	public boolean removeData(T t) {
		ArrayList<T> datas = getDataSource();
		if(datas != null) {
			boolean result = datas.remove(t);
			setDataSource(datas);
			return result;
		}
		return false;
	}
	
	/**
	 * 本地数据源移除数据
	 * @param index
	 * @return
	 */
	public T removeData(int index) {
		ArrayList<T> datas = getDataSource();
		if(datas != null) {
			T t = datas.remove(index);
			setDataSource(datas);
			return t;
		}
		return null;
	}
	
	/**
	 * 本地数据源移除全部数据
	 * @return
	 */
	public boolean clearData() {
		ArrayList<T> datas = getDataSource();
		if(datas != null) {
			datas.clear();
			setDataSource(datas);
			return true;
		}
		return false;
	}
	
//	@Override
//	public void setOnScrollListener(OnScrollListener l) {
//		mScrollListener = l;
//	}

	protected int getLayoutResId(int position, T t, int itemViewType) {
		return 0;
	}
	
	protected ILayoutView<T> getLayoutView(int position, T t, int itemViewType) {
		return null;
	}
	
}